package com.suo.controller;

import com.suo.pojo.Admin;
import com.suo.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin")
public class AdminAction {

    private AdminService adminService;

    @Autowired
    public void setAdminService(AdminService adminService) {
        this.adminService = adminService;
    }

    @RequestMapping("/login")
    public ModelAndView login(String name, String pwd, HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();

        Admin admin = adminService.login(name, pwd);

        if (admin == null) {
            modelAndView.setViewName("login");
            modelAndView.addObject("errmsg","用户名或密码不正确!!!");
        }
        else {
            modelAndView.setViewName("main");
            modelAndView.addObject("admin",admin);
            session.setAttribute("admin",admin);
        }


        return modelAndView;
    }
}
